<template>
  <basic-container>
    <el-container>
      <el-aside width="260px">
        <avue-tree :option="treeOption" :data="treeData" @node-click="treeNodeClick">
          <template slot-scope="scope" slot="addBtn">
            
          </template>
        </avue-tree>
      </el-aside>
      <el-main>
        <avue-crud :option="option"
                  :table-loading="loading"
                  :data="data"
                  ref="crud"
                  v-model="form"
                  :defaults.sync="showColumn"
                  :permission="permissionList"
                  :page.sync="page"
                  :before-open="beforeOpen"
                  @sortable-change="sortableChange"
                  @row-del="rowDel"
                  @row-update="rowUpdate"
                  @row-save="rowSave"
                  @refresh-change="refreshChange"
                  @search-change="searchChange"
                  @search-reset="searchReset"
                  @selection-change="selectionChange"
                  @current-change="currentChange"
                  @size-change="sizeChange"
                  @filter="filterChange"
                  @sort-change="sortChange"
                  @row-dblclick="handleRowDBLClick"               
                  @on-load="onLoad">
          <template slot-scope="scope" slot="menuLeft">
            <el-button type="primary"
                      size="small"
                      icon="el-icon-plus"
                      v-if="permission.user_user_batch_add"
                      @click.stop="openBatchAddDialog">批量新增
            </el-button>
            <el-button type="primary"
                   size="small"
                   icon="el-icon-refresh-right"
                   v-if="permission.user_user_edit"
                   @click.stop="resetPassword">重置密码
            </el-button>
            <el-button type="danger"
                      size="small"
                      icon="el-icon-delete"
                      v-if="permission.user_user_delete"
                      @click.stop="handleDelete">删 除
            </el-button>
          </template>
          <template slot-scope="scope" slot="menu">
            <el-button type="text"
                      size="small"
                      icon="el-icon-setting"
                      v-if="permission.user_user_edit"
                      @click="openSettingDialog(scope.row)">个性设置
            </el-button>
            <el-button type="text"
                      size="small"
                      icon="el-icon-user"
                      v-if="permission.user_user_edit"
                      @click="openRoleDialog(scope.row)">角色
            </el-button>
          </template>
          <template slot="menuRight">
            <el-button type="default" v-if="permission.user_user_download" @click.stop="exportExcel"  title="下载" icon="el-icon-download" circle size="small"></el-button>
          </template>
        </avue-crud>
      </el-main>
    </el-container> 
    <!-- 
      批量添加数据对话框
     -->
    <el-dialog title="批量导入" width='80%' :close-on-click-modal='false' :visible.sync="batchAddDialog">
      <el-tag>
        id(空)，
        租户ID(空)，
        用户名，
        真实姓名，
        昵称，
        所属机构ID，
        所属机构名称，
        所属部门ID，
        所属部门名称，
        公司名称，
        职位，
        性别，
        年龄，
        电话号码，
        手机号码，
        邮箱，
        头像链接，
        状态
      </el-tag>
      <el-input
        type="textarea"
        :autosize="{ minRows: 10, maxRows: 20}"
        placeholder="请直接从excel粘贴内容"
        v-model="importDataArea">
      </el-input>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" type="primary" id="copyBtn" @click="handleImport($event)" data-clipboard-target="#pathText">开始导入</el-button>
        <el-button size="small" @click="batchAddDialog = false">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 
      添加角色对话框
     -->
    <el-dialog title="选择用户角色" width="60%"  :close-on-click-modal='false' @close="roleDialogClosed" :visible.sync="roleDialog">
        <div v-if="fullscreenLoading" style="width:100%; text-align:center;font-size:30px;top:100px;position:absolute;z-index:5000">
          <i  class="el-icon-loading"></i>
        </div>
        <el-transfer v-model="roleValue" 
          filterable
          :titles="['可选角色', '已选角色']"
          :button-texts="['移除', '添加 ']"
          @change="roleChanged"
          :data="roleData">
        </el-transfer>
        <div slot="footer" class="dialog-footer">
        <el-button size="small" type="primary" id="copyBtn" @click="handleAddUserRole($event)" >确定</el-button>
        <el-button size="small" @click="roleDialogClosed">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 
      个人设置信息对话框
     -->
    <el-dialog title="个人设置信息" width="70%"  :close-on-click-modal='false' @close="settingDialogClosed" :visible.sync="settingDialog">
      <avue-form ref="form" v-model="settingForm" :option="settingOption" @submit="settingSubmit">
      </avue-form>
    </el-dialog>
  </basic-container>
</template>

<script>
import {saveOrUpdateSysUserSetting,saveSysUserSetting,querySysUserSetting} from '@/api/system/sysUserSettingApi';
import {saveOrUpdateSysUserRoleRelationList} from '@/api/system/sysUserRoleRelationApi';
import {querySysRoleListPage,querySysRoleListByUserId} from '@/api/system/sysRoleApi';
import {querySysOrgInfoListPage} from '@/api/system/sysOrgInfoApi';
import {restAdminPassword,exportSysUserInfoExcel,saveOrUpdateSysUserInfo,saveOrUpdateSysUserInfoList,saveSysUserInfo,saveSysUserInfoList,deleteSysUserInfo,querySysUserInfo,querySysUserInfoListPage,updateSysUserInfo,deleteSysUserInfoList} from '@/api/system/sysUserInfoApi';
import {mapGetters} from 'vuex';
import { reqCommonMsg } from '@/util/reqCommonMsg';
import {filterFormat} from '@/util/util';
export default {
  data() {
    return {
      //角色对话框
      fullscreenLoading: false,
      roleDialog: false,
      roleData: [],
      roleValue: [],
      oldRoleValue: [],
      roleChangeList:[],
      currentRow: {},

      importDataArea: '',
      batchAddDialog: false,
      form: {},
      selectionList: [],
      loading: true, 
      showColumn: [],
      option: {
        labelPosition: 'right',
        labelSuffix: '：',
        labelWidth: 120,
        dialogTop: '50',
        indexFixed:true, 
        stripe:true,
        calcHeight: 80,
        searchShow: true,
        searchMenuSpan: 6,
        tip: false,
        border: true,
        index: true,
        selection: true,
        addBtn: true,
        viewBtn: false,
        delBtn: false,
        cancelBtn: true,
        printBtn:false,
        saveBtn: true,
        filterBtn:true,          
        menuWidth: 220,
        dialogMenuPosition: 'center',
        dialogDirection:'rtl',
        dialogType:'dialog',
        dialogFullscreen:false,
        dialogClickModal:false,
        dialogWidth: '70%',
        highlightCurrentRow:  true,
        searchShow:true,
        sortable: true,
        column: [
          {
            label: '用户ID',
            prop: 'id', 
            type: 'input', 
            search: false,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            span: 12,
            sortable: false,
            hide: false,
            width:160,
            display: false,
            overHidden: true,
            rules: [{
              required:  false,
              message: '请输入内容',
              trigger: 'blur'
            }],
            dicData:[]
          },
          {
            label: '用户名',
            prop: 'userName', 
            type: 'input', 
            search: true,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            span: 12,
            sortable: false,
            hide: false,
            width:100,
            overHidden: true,
            rules: [{
              required:  true,
              message: '请输入内容',
              trigger: 'blur'
            }],
            dicData:[]
          },
          {
            label: '真实姓名',
            prop: 'realName', 
            type: 'input', 
            search: true,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            span: 12,
            sortable: false,
            hide: false,
            overHidden: true,
            rules: [{
              required:  true,
              message: '请输入内容',
              trigger: 'blur'
            }],
            dicData:[]
          },
          {
            label: '昵称',
            prop: 'nickName', 
            type: 'input', 
            search: false,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            span: 12,
            sortable: false,
            hide: true,
            overHidden: true,
            rules: [{
              required:  true,
              message: '请输入内容',
              trigger: 'blur'
            }],
            dicData:[]
          },
          {
            label: '用户类型',
            prop: 'userType', 
            type: 'select', 
            search: false,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            span: 12,
            sortable: false,
            hide: true,
            overHidden: true,
            value:'1',
            rules: [{
              required:  true,
              message: '请输入内容',
              trigger: 'blur'
            }],
            dicData:[
              {
                label:'自有员工',
                value:'1'
              },{
                label:'借调人员',
                value:'2'
              },
              {
                label:'外包人员',
                value:'3'
              },
              {
                label:'其他',
                value:'4'
              }
            ]
          },
          {
            label: '所属机构号',
            prop: 'belongOrgNo', 
            type: 'input', 
            search: false,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            span: 12,
            sortable: false,
            overHidden: true,
            hide: true,
            rules: [{
              required:  true,
              message: '请输入内容',
              trigger: 'blur'
            }],
            props: {
              label: 'orgName',
              value: 'orgNo'
            },
            type:'tree',
            dicData: []     
          },
          {
            label: '所属机构名称',
            prop: 'belongOrgName', 
            search: false,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            span: 12,
            sortable: false,
            hide: true,
            overHidden: true,
            display: false,
            rules: [{
              required:  false,
              message: '请输入内容',
              trigger: 'blur'
            }],
            type:'input',
            dicData: [],          
          },
          {
            label: '公司名称',
            prop: 'companyName', 
            type: 'input', 
            search: false,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            span: 12,
            sortable: false,
            hide: true,
            overHidden: true,
            rules: [{
              required:  false,
              message: '请输入内容',
              trigger: 'blur'
            }],
            dicData:[]
          },
          {
            label: '职位',
            prop: 'postName', 
            type: 'input', 
            search: false,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            span: 12,
            sortable: false,
            hide: true,
            overHidden: true,
            rules: [{
              required:  false,
              message: '请输入内容',
              trigger: 'blur'
            }],
            dicData:[]
          },
          {
            label: '性别',
            prop: 'sex', 
            type: 'input', 
            search: false,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            span: 12,
            sortable: false,
            hide: true,
            overHidden: true,
            rules: [{
              required:  false,
              message: '请输入内容',
              trigger: 'blur'
            }],
            dicData:[]
          },
          {
            label: '年龄',
            prop: 'age', 
            type: 'input', 
            search: false,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            span: 12,
            sortable: false,
            hide: true,
            overHidden: true,
            rules: [{
              required:  false,
              message: '请输入内容',
              trigger: 'blur'
            }],
            dicData:[]
          },
          {
            label: '电话号码',
            prop: 'phoneNo', 
            type: 'input', 
            search: true,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            span: 12,
            sortable: false,
            hide: true,
            overHidden: true,
            rules: [{
              required:  false,
              message: '请输入内容',
              trigger: 'blur'
            }],
            dicData:[]
          },
          {
            label: '手机号码',
            prop: 'mobileNo', 
            type: 'input', 
            search: false,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            span: 12,
            sortable: false,
            hide: false,
            overHidden: true,
            rules: [{
              required:  true,
              message: '请输入内容',
              trigger: 'blur'
            }],
            dicData:[]
          },
          {
            label: '邮箱',
            prop: 'email', 
            type: 'input', 
            search: false,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            span: 12,
            sortable: false,
            hide: true,
            overHidden: true,
            rules: [{
              required:  false,
              message: '请输入内容',
              trigger: 'blur'
            }],
            dicData:[]
          },
          {
            label: '头像链接',
            prop: 'headImgUrl', 
            type: 'input', 
            search: false,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            span: 12,
            sortable: false,
            hide: true,
            overHidden: true,
            rules: [{
              required:  false,
              message: '请输入内容',
              trigger: 'blur'
            }],
            dicData:[]
          },
          {
            label: '用户状态',
            prop: 'validFlag', 
            type: 'switch', 
            search: false,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            span: 12,
            sortable: false,
            hide: false,
            overHidden: true,
            value: '1',
            rules: [{
              required:  false,
              message: '请输入内容',
              trigger: 'blur'
            }],
            dicData:[
              {
                label: '注销',
                value: '0'
              },
              {
                label: '正常',
                value: '1'
              },
            ]
          },
        ]
      },
      data: [],
      // 个人设置区
      settingDialog: false,
      settingForm: {},
      settingOption: {
        labelPosition: 'right',
        labelSuffix: '：',
        labelWidth: 120,
        dialogTop: '50',
        indexFixed:true, 
        stripe:true,
        calcHeight: 80,
        searchShow: true,
        searchMenuSpan: 6,
        tip: false,
        border: true,
        index: true,
        selection: true,
        addBtn: true,
        viewBtn: true,
        delBtn: true,
        cancelBtn: true,
        printBtn:false,
        emptyBtn: false,
        saveBtn: true,
        filterBtn:true,          
        menuWidth: 200,
        submitText: '保存',
        dialogMenuPosition: 'center',
        dialogDirection:'rtl',
        dialogType:'dialog',
        dialogFullscreen:false,
        dialogClickModal:false,
        dialogWidth: '70%',
        highlightCurrentRow:  true,
        searchShow:true,
        sortable: true,
        column: [
          {
            label: 'ID',
            prop: 'id', 
            type: 'input', 
            search: false,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            span: 12,
            sortable: false,
            hide: false,
            overHidden: true,
            readonly: true,
            rules: [{
              required:  false,
              message: '请输入内容',
              trigger: 'blur'
            }],
            dicData:[]
          },
          {
            label: '租户ID',
            prop: 'tenantId', 
            type: 'input', 
            search: false,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            span: 12,
            sortable: false,
            hide: false,
            overHidden: true,
            readonly: true,
            rules: [{
              required:  false,
              message: '请输入内容',
              trigger: 'blur'
            }],
            dicData:[]
          },
          {
            label: '用户名',
            prop: 'userName', 
            type: 'input', 
            search: true,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            span: 12,
            sortable: false,
            hide: false,
            overHidden: true,
            readonly: true,
            rules: [{
              required:  false,
              message: '请输入内容',
              trigger: 'blur'
            }],
            dicData:[]
          },
          {
            label: '真实姓名',
            prop: 'realName', 
            type: 'input', 
            search: true,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            span: 12,
            sortable: false,
            hide: false,
            overHidden: true,
            readonly: true,
            rules: [{
              required:  false,
              message: '请输入内容',
              trigger: 'blur'
            }],
            dicData:[]
          },
          {
            label: '系统版本',
            prop: 'systemVersionNo', 
            type: 'number', 
            search: false,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            span: 12,
            sortable: false,
            hide: false,
            overHidden: true,
            rules: [{
              required:  false,
              message: '请输入内容',
              trigger: 'blur'
            }],
            dicData:[]
          },
          {
            label: '顶部菜单版本',
            prop: 'topMenuVersionNo', 
            type: 'number', 
            search: false,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            span: 12,
            sortable: false,
            hide: false,
            overHidden: true,
            rules: [{
              required:  false,
              message: '请输入内容',
              trigger: 'blur'
            }],
            dicData:[]
          },
          {
            label: '菜单版本',
            prop: 'menuVersionNo', 
            type: 'number', 
            search: false,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            span: 12,
            sortable: false,
            hide: false,
            overHidden: true,
            rules: [{
              required:  false,
              message: '请输入内容',
              trigger: 'blur'
            }],
            dicData:[]
          },
          {
            label: '重置编号',
            prop: 'resetNo', 
            type: 'number', 
            search: false,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            span: 12,
            sortable: false,
            hide: false,
            overHidden: true,
            rules: [{
              required:  false,
              message: '请输入内容',
              trigger: 'blur'
            }],
            dicData:[]
          },
          {
            label: '消息编号',
            prop: 'msgNo', 
            type: 'number', 
            search: false,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            span: 12,
            sortable: false,
            hide: false,
            overHidden: true,
            rules: [{
              required:  false,
              message: '请输入内容',
              trigger: 'blur'
            }],
            dicData:[]
          }, 
          {
            label: '通知消息',
            prop: 'noticeMsg', 
            type: 'textarea', 
            search: false,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            span: 24,
            sortable: false,
            hide: false,
            overHidden: true,
            maxRows:2,
            minRows:2,
            rules: [{
              required:  false,
              message: '请输入内容',
              trigger: 'blur'
            }],
            dicData:[]
          }, 
          {
            label: '读取标志',
            prop: 'readFlag', 
            type: 'switch', 
            search: false,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            span: 12,
            sortable: false,
            hide: false,
            overHidden: true,
            rules: [{
              required:  false,
              message: '请输入内容',
              trigger: 'blur'
            }],
            dicData:[
              {
                label:'未读取',
                value:'0'
              },{
                label:'已读取',
                value:'1'
              }
            ]
          },
          {
            label: '个人设置内容',
            prop: 'personalSettingContent', 
            type: 'textarea', 
            search: false,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            span: 24,
            sortable: false,
            hide: false,
            overHidden: true,
            rules: [{
              required:  false,
              message: '请输入内容',
              trigger: 'blur'
            }],
            dicData:[]
          },
          {
            label: '个人设置内容1',
            prop: 'personalSettingOneContent', 
            type: 'textarea', 
            search: false,
            searchMmultiple: false,
            searchLabelWidth: 100,
            multiple: false,
            span: 24,
            sortable: false,
            hide: false,
            overHidden: true,
            maxRows: 2,
            minRows:2,
            rules: [{
              required:  false,
              message: '请输入内容',
              trigger: 'blur'
            }],
            dicData:[]
          }
        ]
      },
      settingData: [],
      // 组织机构树形区
      checkedNode: '',
      currentOrgNo: '',
      orgType: '1',
      treeData:[],
      treeOption:{
        addBtn: false,
        menu: false,
        defaultExpandAll:false,
        formOption:{},
        lazy: true,
        treeLoad: (node, resolve) => {
          //this.$console.log('node======',node) 
          this.singleBody = {
            parentOrgNo: node.data.orgNo
          } 
          if (node.level === 0) {
            this.singleBody = {
              parentOrgNo: this.website.orgNo.treeOrgNo,
              orgType:this.orgType
            }
          }
          this.reqParams =  { 
            singleBody: this.singleBody,
            listBody: [],  
            extBody: {},  
            sqlCondition: [],  
            orderRule: [{prop:'order_seq',order:'ascending'}],
            currentPage: 0,
            pageSize: 99999,
            total: 100 
          }
          this.commonData = reqCommonMsg(this.reqParams)
          querySysOrgInfoListPage(this.commonData).then(res => {
            let data = res.data.body.listBody;
            data.forEach(element => {
              if(!element.hasChildren){
                element.leaf = true
              }
            });
            return resolve(data)
          }, error => {});
        },
        props: {
          label: 'orgName',
          value: 'orgNo'
        },
      },
      // 请求参数区
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      reqParams: {
        singleBody: {},
        listBody: [{}],
        sqlCondition:[],
        orderRule: {},
        currentPage: 1,
        pageSize: 10
      },
      commonData: null,
      conditions: [],
      orderRules: [],
      singleBody: {}
    };
  },
  computed: {
    ...mapGetters(['website','userInfo','orgTree','permission']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.user_user_add, false),
        viewBtn: this.vaildData(this.permission.user_user_view, false),
        delBtn: this.vaildData(this.permission.user_user_delete, false),
        editBtn: this.vaildData(this.permission.user_user_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push({id : ele.id});
      });
      return ids;
    } 
  },
  methods: {
    /**
     * 重置密码
     */
    resetPassword(){
      if (this.selectionList.length !== 1) {
        this.$message.warning('请选择一个租户数据');
        return;
      }
      this.reqParams.singleBody = {
        id: this.ids[0].id,
        password: this.$toolUtil.encodeMd5(this.userInfo.tenantId),
        tenantId: this.userInfo.tenantId
      }
      this.commonData = reqCommonMsg(this.reqParams)
      restAdminPassword(this.commonData).then(() => {
        this.singleBody={};
        this.$message({
          customClass:'zZindex',
          type: 'success',
          message: '用户密码重置成功, 初始密码为：（'+this.userInfo.tenantId+'）!'
        });
      }, error => {
        //this.$console.log(error);
      });
    },
    /**
     * 打开个人设置对话框
     */
    openSettingDialog(row){
      this.settingForm = {};
      this.settingDialog = true;
      let singleBody ={
        id: row.id
      } 
      this.reqParams =  {
        singleBody: singleBody,
        currentPage: 1, 
        pageSize: this.website.maxPageSize
      }
      this.commonData = reqCommonMsg(this.reqParams)
      querySysUserSetting(this.commonData).then(res => {
        this.settingForm = res.data.body.singleBody;
      }, error => {
        this.settingForm = {};
      });
    },
    /**
     * 保存设置
     */
    settingSubmit(form,done){
      if(form.id == ''){
        this.$message({
          customClass:'zZindex',
          type: 'warning',
          message: '数据缺失，不能保存!'
        });
        return false;
      }
      this.reqParams.singleBody = form;
      this.commonData = reqCommonMsg(this.reqParams)
      saveOrUpdateSysUserSetting(this.commonData).then(() => {
        done();
        this.singleBody={}; 
        this.$message({
          customClass:'zZindex',
          type: 'success',
          message: '保存成功!'
        });
        this.settingDialog = false;

      }, error => {
        this.$message({
          customClass:'zZindex',
          type: 'error',
          message: '操作失败!'
        });
        //this.$console.log(error);
        this.settingDialog = false;
        done();
      });       
      //this.$message.success(JSON.stringify(form));
    },
    /**
     * 打开角色对话框
     */
    async openRoleDialog(row){
      if (row.status !== '1') {
        this.$message.warning('当前用户已注销！');
        return;
      }
      this.currentRow = row;
      this.roleDialog = true;
      this.fullscreenLoading = true;
      //获取所有角色列表
      this.reqParams =  {
        singleBody: this.singleBody,
        currentPage: 1, 
        pageSize: this.website.maxPageSize
      }
      this.commonData = reqCommonMsg(this.reqParams)
      await querySysRoleListPage(this.commonData).then(res => {
        //this.$console.log('querySysRoleListPage = ',res)
        res.data.body.listBody.forEach(element => {
          this.roleData.push({
            key: element.id,
            label: '['+element.roleGroupName+']'+element.roleNickName,
            disabled: (element.status == '0') ? true: false
          });
        });
      }, error => {
      });

      //获取当前用户的角色
      this.singleBody = {
        userId: row.id
      }
      this.reqParams =  {
        singleBody: this.singleBody,
        currentPage: 1, 
        pageSize: this.website.maxPageSize
      }
      this.commonData = reqCommonMsg(this.reqParams)
      await querySysRoleListByUserId(this.commonData).then(res => {
        //this.$console.log('querySysRoleListByUserId = ',res)
        res.data.body.listBody.forEach(element => {
          this.roleValue.push(element.id);
        });
        this.oldRoleValue = this.deepClone(this.roleValue)
        this.fullscreenLoading = false;
      }, error => {
      });
    },
    /**
     * 角色对话框结束时回调
     */
    roleDialogClosed(done){
      this.roleDialog = false;
      this.roleData = [];
      this.roleValue = [];
    },
    /**
     * 穿梭框选择发生变化时
     */
    roleChanged(data,direct,changeValue){
    },
    /**
     * 保存角色
     */
    handleAddUserRole(){
      this.fullscreenLoading = true;
      // 前后角色对比，给新增和删除的打上标识
      let resultList = [];
      // 原列表不存在的表示新增的元素
      let inOldValue = false;
      let inNewValue = false;
      this.roleValue.forEach(element => {
        inOldValue = false;
        for(var i = 0 ; i < this.oldRoleValue.length; i++){
          if (element == this.oldRoleValue[i]) {
            inOldValue = true;
            i = this.oldRoleValue.size + 1;
          }
        }
        //若没找到表示新增元素
        if(!inOldValue){
          resultList.push({
            userId: this.currentRow.id,
            roleId: element,
            validFlag: '1'
          })
        }
      });

      //新列表中不存在 表示 删除元素
      this.oldRoleValue.forEach(element => {
        inNewValue = false;
        for(var i = 0 ; i < this.roleValue.length; i++){
          if (element == this.roleValue[i]) {
            inNewValue = true;
            i = this.roleValue.size + 1;
          }
        }
        //若没找到表示删除元素
        if(!inNewValue){
          resultList.push({
            userId: this.currentRow.id,
            roleId: element,
            validFlag: '0'
          })
        }
      });

      if(resultList.length > 0){
        this.reqParams =  {
          transCode: '0001',
          listBody: resultList
        }
        this.commonData = reqCommonMsg(this.reqParams)
        saveOrUpdateSysUserRoleRelationList(this.commonData).then(() => {
          this.$message({
            customClass:'zZindex',
            type: 'success',
            message: '保存成功!'
          });
          this.roleDialog = false;
          this.fullscreenLoading = false;
        }, error => {
          //this.$console.log(error);
        });
      }
      else{
        this.$message({
          customClass:'zZindex',
          type: 'warning',
          message: '角色未发生变化!'
        });
        this.fullscreenLoading = false;
      }
    },
    /**
     * 机构树形菜单点击时触发事件
     */
    treeNodeClick(data){
      this.currentOrgNo =  data.orgNo;
      this.onLoad(this.page)
    },
    /**
     * 加载列表数据
     */
    onLoad(page) {
      this.singleBody.belongOrgNo=this.currentOrgNo 
      this.loading = true;
      this.reqParams =  {
        singleBody: this.singleBody,
        listBody: [{}],
        sqlCondition: this.conditions,
        orderRule: this.orderRules,
        currentPage: page.currentPage, 
        pageSize: page.pageSize
      }
      this.commonData = reqCommonMsg(this.reqParams)
      querySysUserInfoListPage(this.commonData).then(res => {
        
        const data = res.data.body;
        this.page.total = data.total;
        this.data = data.listBody;
        this.loading = false;
        this.selectionClear();
      }, error => {
        this.loading = false; 
      });
    },
    /**
     * 新增一条记录
     */
    rowSave(row, done, loading) {
      this.reqParams.singleBody = row;
      this.reqParams.singleBody.belongOrgName=row.$belongOrgNo;
      this.reqParams.singleBody.password = this.$toolUtil.encodeMd5(this.userInfo.tenantId);
      this.commonData = reqCommonMsg(this.reqParams)
      saveSysUserInfo(this.commonData).then(() => {
        done();
        this.singleBody={};
        this.onLoad(this.page);
        this.$message({
          customClass:'zZindex',
          type: 'success',
          message: '操作成功!'
        });
      }, error => {
        //this.$console.log(error);
        loading();
      });         
      //this.$console.log("row = ",row ,loading)
    },
    /**
     * 修改一条记录
     */
    rowUpdate(row, index, done, loading) { 
      this.reqParams.singleBody = row;
      this.reqParams.singleBody.belongOrgName=row.$belongOrgNo;
      this.commonData = reqCommonMsg(this.reqParams)
      updateSysUserInfo(this.commonData).then(() => {
        done();
        this.singleBody={}
        this.onLoad(this.page);
        this.$message({
          customClass:'zZindex',
          type: 'success',
          message: '操作成功!'
        });
      }, error => {
        //this.$console.log(error);
        loading();
      });
      //this.$console.log('loading = ',loading)
    },
    /**
     * 删除一条记录
     */
    rowDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.reqParams.singleBody = row;
          this.commonData = reqCommonMsg(this.reqParams);
          return deleteSysUserInfo(this.commonData);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            customClass:'zZindex',
            type: 'success',
            message: '操作成功!'
          });
        }).catch(() => {});
    },
    /**
     * 双击打开详细窗口
     */
    handleRowDBLClick (row, event) {
      if(this.permission.user_user_dbclick){
        this.$refs.crud.rowView(row,row.$index);
      }
    },
    /**
     * 批量新增记录
     */
    openBatchAddDialog(){
      this.batchAddDialog = true;
    },
    // 
    handleImport() {
      this.loading=true;
      var dataList = new Array();
      if(this.importDataArea){
        var lineList = this.importDataArea.split('\n')
        lineList.forEach((element,index) => {
          if(element.trim() != ''){
            var field = element.split('\t')
            var fieldList = {
              userName:field[2],
              realName:field[3],
              nickName:field[4],
              belongOrgNo:field[5],
              belongOrgName:field[6],
              belongDeptId:field[7],
              belongDeptName:field[8],
              companyName:field[9],
              postName:field[10],
              sex:field[11],
              age:field[12],
              phoneNo:field[13],
              mobileNo:field[14],
              email:field[15],
              headImgUrl:field[16],
              status:field[17],
              password: this.$toolUtil.encodeMd5('123456')
            }
            dataList[index] = fieldList
            //this.$console.log('fieldList = ',fieldList)
          }
        });
        //批量写入记录
        this.reqParams.listBody = dataList;
        this.commonData = reqCommonMsg(this.reqParams)
        saveSysUserInfoList(this.commonData).then(() => {
          this.loading=false;
          this.onLoad(this.page);
          this.$message({
            customClass:'zZindex',
            type: 'success',
            message: '操作成功!'
          });
        }, error => {
          //this.$console.log(error);
          this.loading=false;
          loading();
        });      
        //this.$console.log('lineList=',dataList)
      }
      else{
        this.loading=false;
        this.$message({
          customClass:'zZindex',
          type: 'warning',
          message: '数据格式不正确!'
        });
      }
    },
    /**
     * 搜索重置
     */
    searchReset() {
      this.conditions =[];
      this.singleBody = {};
      this.onLoad(this.page);
    },
    /**
     * 搜索
     */
    searchChange(params, done) {
      this.singleBody = params
      this.page.currentPage = 1;
      this.onLoad(this.page);
      done();
    },
    refreshChange(){
      this.onLoad(this.page);
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    /**
     * 处理批量删除
     */
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning('请选择至少一条数据');
        return;
      }
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.reqParams.listBody = this.ids;
          this.commonData = reqCommonMsg(this.reqParams);
          return deleteSysUserInfoList(this.commonData);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            customClass:'zZindex',
            type: 'success',
            message: '操作成功!'
          });
          this.$refs.crud.toggleSelection();
        }).catch(() => {});
    },
    /**
     * 拷贝记录
     */
    handleCopy(row){
      this.$confirm('确定复制数据?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading=true;
        let copyValue=row;
        copyValue.id='0';
        copyValue.dataSign='0'
        copyValue.tableName='拷贝 '+ copyValue.tableName; 
        this.reqParams.singleBody = copyValue;
        this.commonData = reqCommonMsg(this.reqParams)
        saveSysToolTables(this.commonData).then(() => {
          this.singleBody={};
          this.onLoad(this.page);
          this.$message({
            customClass:'zZindex',
            type: 'success',
            message: '操作成功!'
          });
          this.loading=false;
        }, error => {
          //this.$console.log(error);
          this.loading=false;
        });
      }).catch(() => {});       
    },
    /**
     * 打开编辑、查询弹窗前查询记录
     */  
    beforeOpen(done, type) {
      this.reqParams =  {
        singleBody: {
          id:this.form.id
        }
      }
      let commonData = reqCommonMsg(this.reqParams)
      if (['edit', 'view'].includes(type)) {
        querySysUserInfo(commonData).then(res => {
          this.form = res.data.body.singleBody;
        });
      }
      else if (['add'].includes(type)) { 
        this.form = { belongOrgNo : this.currentOrgNo}
      }
      //注意：不能放在mounted里，或出现加载不成功
      this.getOrgTree();
      done();
    },
    /**
     * 翻页查询
     */
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    /**
     * 更改每页条数
     */
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    /**
     * 自定义查询条件
     */
    filterChange(result) {
      this.page.currentPage = 1;
      this.conditions = filterFormat(result)
      this.onLoad(this.page);
    },
    /**
     * 列表排序
     */
    sortChange(val){
      this.orderRules=val
      this.onLoad(this.page);
    },
  
    /**
     * 导出Excel
     */
    exportExcel() {
      this.reqParams =  {
        singleBody: this.singleBody,
        listBody: [{}],
        sqlCondition: this.conditions,
        orderRule: this.orderRules,
        currentPage:1, 
        pageSize: 100
      }
      //this.$console.log('reqParams = ',this.reqParams)
      this.commonData = reqCommonMsg(this.reqParams)
      exportSysUserInfoExcel(this.commonData);
    },
    /**
     * 获取树形菜单
     */
    getOrgTree(){
      const column = this.findObject(this.option.column, 'belongOrgNo');
      this.orgTree.forEach((element,index) => {
        if(element.orgType == '1'){
          //this.$console.log('element == ',element);
          column.dicData = [element];
        }
      });
    },
  },
  created() {
    this.showColumn = this.$getStore({name:  this.userInfo.userId +'sysUserInfoColumn'});
  },
  watch: {
    showColumn: {
      handler(newValue, oldValue) {
        this.$setStore({name:  this.userInfo.userId+'sysUserInfoColumn', content: this.showColumn});
      },
      deep: true
      //this.$setStore({name: this.userInfo.userId +'sysUserInfoColumn', content: this.showColumn});
    }
  }
};
</script>
<style>
.el-main {
    margin-left: 5px !important;  
    border-left: 10px solid #f3f3f3 !important;  
}
</style>
